<template>
    <div class="body-container">
        <div class="main-container">
            <el-card class="box-card" style="width: 100%;">
                <div>
                    <h1 class="text-center"><span style="color:#303133;">TTFE</span><small
                            style="padding-left: 10px; color:#409EFF">考试通</small></h1>
                    <h3 class="text-center"><small style="color:#909399;">最权威的考试网站</small></h3>
                </div>

                <el-tabs v-model="activeName">
                    <el-tab-pane label="学生登录" name="first">
                        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="70"
                                 class="demo-ruleForm">
                            <el-form-item label="用户名" prop="username">
                                <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                            </el-form-item>
                            <el-form-item label="密码" prop="password">
                                <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                            </el-form-item>
                            <el-form-item style="margin: 0 auto;">
                                <el-button type="primary" @click="stuLogin('ruleForm')">提交</el-button>
                                <el-button @click="resetForm('ruleForm')" style="margin-right: 10px;">重置</el-button>
                                <router-link to="/user/register" class="text-decoration-none">
                                    <el-button>注册账号</el-button>
                                </router-link>
                            </el-form-item>
                        </el-form>
                    </el-tab-pane>

                    <el-tab-pane label="老师登录" name="second">


                    </el-tab-pane>
                </el-tabs>
            </el-card>
        </div>
    </div>
</template>

<script>
    import {request} from "../../api/request";

    export default {
        name: "Login",
        data() {
            return {
                activeName: 'first',
                ruleForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请选择密码', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            stuLogin(form) {
                let that = this;
                this.$refs[form].validate((valid) => {
                    if (valid) {
                        request({
                            method: 'post',
                            params: {
                                username: that.ruleForm.username,
                                password: that.ruleForm.password
                            },
                            url: '/student/login'
                        }).then(function (data) {
                            data = data.data;
                            if (data.code === 400) {
                                that.$message.error("登录的账号或密码错误");
                            } else {
                                var token = data.token;
                                window.localStorage.setItem("token", token);
                                that.$message.success("登录成功");
                                that.$store.commit('updateStudentInfo', data.data);
                                setTimeout(_ => {
                                    that.$router.push('/student/index');
                                }, 400);
                            }
                        }).catch(function (err) {
                            that.$message.error("登录时发生错误");
                        });
                    } else {
                        return false;
                    }
                });
            },
            teaLogin() {
                let that = this;
                request({
                    method: 'post',
                    params: {
                        username: that.form.username,
                        password: that.form.password,
                    },
                    url: '/teacher/login'
                }).then(function (data) {
                    console.log(data);
                }).catch(function (err) {
                    console.log(err);
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>
    .body-container {
        width: 100%;
        height: 100%;
    }

    .main-container {
        margin: 0 auto;
        width: 400px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }
</style>